import {
    FindTemplateList,
    FindTemplateListWithPage,
    CreateTemplate,
    DeleteTemplate,
    FindTemplate,
    UpdateTemplate
} from '@/services/product/productDetailTemplate';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'productDetailTemplate',

    state: {
        templateList:[],
        tempDetail:[],
        allTempList:[]
    },
    effects: {
        *FindTemplateList({ payload, callback }, { call, put }) {
            const data = yield call(FindTemplateList, payload)
            yield catchException(function* (){
              if (data.Code === 0) {
                yield put({
                    type: 'saveFindTemplateList',
                    payload: data.Data||[],
                });
                callback && callback.success && callback.success(data);
              }else{
                callback && callback.error && callback.error(data);
              }
            })

        },
        *FindTemplateListWithPage({ payload, callback }, { call, put }) {
          const data = yield call(FindTemplateListWithPage, payload)
          yield catchException(function* (){
            if (data.Code === 0) {
              yield put({
                  type: 'saveFindTemplateListWithPage',
                  payload: data.Data.DataList||[],
              });
              callback && callback.success && callback.success(data);
            }else{
              callback && callback.error && callback.error(data);
            }
          })

        },
        *CreateTemplate({ payload, callback }, { call, put }) {
            const data = yield call(CreateTemplate, payload)
            yield catchException(function* (){
              if (data.Code === 0) {
                callback && callback.success && callback.success(data);
            }else{
                callback && callback.error && callback.error(data);
            }
            })

        },
        *DeleteTemplate({ payload, callback }, { call, put }) {
            const data = yield call(DeleteTemplate, payload)
            yield catchException(function* (){
              if (data.Code === 0) {
                callback && callback.success && callback.success(data);
              }else{
                  callback && callback.error && callback.error(data);
              }
            })

        },
        *FindTemplate({ payload, callback }, { call, put }) {
            const data = yield call(FindTemplate, payload)
            yield catchException(function* (){
              if (data.Code === 0) {
                yield put({
                    type: 'saveFindTemplate',
                    payload: data.Data.JsonData||[],
                });
                callback && callback.success && callback.success(data);
              }else{
                  callback && callback.error && callback.error(data);
              }
            })

        },
        *UpdateTemplate({ payload, callback }, { call, put }) {
            const data = yield call(UpdateTemplate, payload)
            yield catchException(function* (){
              if (data.Code === 0) {
                callback && callback.success && callback.success(data);
              }else{
                  callback && callback.error && callback.error(data);
              }
            })

        },
    },
    reducers: {
        saveFindTemplateList(state, { payload }) {
            return {
                ...state,
                allTempList: payload,
            };
        },
        saveFindTemplateListWithPage(state, { payload }) {
            return {
                ...state,
                templateList: payload,
            };
        },
        saveFindTemplate(state, { payload }) {
            return {
                ...state,
                tempDetail: payload,
            };
        },
    },
}
